using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ExperimentManager {
  public partial class FormMain : Form {
    public FormMain() {
      InitializeComponent();
    }

    private void btnBrowseServer_Click(object sender, EventArgs e) {
      if (openFileDialogExe.ShowDialog() == DialogResult.OK) {
        tbServerPath.Text = openFileDialogExe.FileName;
      }
    }

    private void btnBrowseClient_Click(object sender, EventArgs e) {
      if (openFileDialogExe.ShowDialog() == DialogResult.OK) {
        tbClientPath.Text = openFileDialogExe.FileName;
      }
    }

    private void button1_Click(object sender, EventArgs e) {
      CheckForIllegalCrossThreadCalls = false;
      Thread t = new Thread(Run);
      t.Start();
    }

    public void Run() {
      button1.Enabled = false;
      
      // nasteni zadanych parametru

      // *2 protoze nastavujeme v kolech, ale potrebujeme v tazich
      Generalizer.SuccessorDepth = int.Parse(tbSuccessorDepth.Text) * 2;
      Generalizer.LMSn = double.Parse(tbLMSn.Text);

      Experiment.ConnectionParams = tbConnectionParams.Text;
      Experiment.ServerPath = tbServerPath.Text;
      Experiment.ClientPath = tbClientPath.Text;
      Experiment.DepthOfSearch = tbDepthOfSearch.Text;

      int numberOfRuns = int.Parse(tbNumerOfRuns.Text);
      int[] currentWeights = new int[6];
      int runNumber = 0;

      currentWeights[0] = int.Parse(tbW1.Text);
      currentWeights[1] = int.Parse(tbW2.Text);
      currentWeights[2] = int.Parse(tbW3.Text);
      currentWeights[3] = int.Parse(tbW4.Text);
      currentWeights[4] = int.Parse(tbW5.Text);
      currentWeights[5] = int.Parse(tbW6.Text);

      using (System.IO.StreamWriter sw = new System.IO.StreamWriter("results.csv")) {
        // spusteni zadaneho poctu experimentu
        for (int i = 0; i < numberOfRuns; i++) {
          runNumber = i;

          try {
            // experiment manager + vykonny system
            Experiment.StartExperimentRun("log_" + runNumber.ToString() + ".xml", currentWeights);
          }
          catch (Exception e) {
            MessageBox.Show("An error ocuured in the 'Experiment manager'.  The application will terminate.\nError details: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
          }

          // Kritik
          List<TrainingExample> trainingExamples = null;
          try {
            trainingExamples = Critic.GetTrainingExamples("log_" + runNumber.ToString() + ".xml");
          } catch (Exception e) {
            MessageBox.Show("An error ocuured in the 'Critic'. The application will terminate.\nError details: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
          }
          
          // generalizer
          try {
            Generalizer.UpdateWeights(ref currentWeights, trainingExamples);
          } catch (Exception e) {
            MessageBox.Show("An error ocuured in the 'Generalizer'. The application will terminate.\nError details: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
          }

          // graficke znazorneni prubehu uceni
          DataGridViewRow row = new DataGridViewRow();
          for (int w = 0; w < currentWeights.Length; w++) {
            DataGridViewTextBoxCell c = new DataGridViewTextBoxCell();
            c.Value = currentWeights[w].ToString();
            row.Cells.Add(c);
          }
          dataGridView1.Rows.Add(row);
          tbRunNumber.Text = (runNumber +1).ToString();

          sw.WriteLine("{0:D}; {1:D}; {2:D}; {3:D}; {4:D}; {5:D};", currentWeights[0], currentWeights[1], currentWeights[2], currentWeights[3], currentWeights[4], currentWeights[5]);

          this.Invalidate();
        }
      }

      button1.Text = "Done";
    }
  }
}